# -*- coding: utf-8 -*-
import scrapy
from fiction.items import FictionItem


class FictionsSpider(scrapy.Spider):
    name = 'fictions'
    allowed_domains = ['readnovel.com']
    start_urls = ['https://www.readnovel.com/chapter/22023237000333502/107748132325951475']

    def parse(self, response):
        # 1, 定位所需要的数据
        # 章节
        item = FictionItem()
        item['theme'] = response.xpath("//*[@class='j_chapterName']/text()").extract_first()
        # 内容
        item['content'] = response.xpath("//*[@class='read-content j_readContent']/p/text()").extract()
        yield item
        # print(item)
        # 2, 翻页
        next_url = "https:" + response.xpath('//*[@id="j_chapterNext"]/@href').extract_first()
        # print(next_url)
        yield scrapy.Request(next_url, callback=self.parse)
